package com.mybank.fundtrans.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mybank.fundtrans.dao.FundDao;
import com.mybank.fundtrans.entity.Fund;
import com.mybank.fundtrans.utils.*;

public class FundDaoImpl implements FundDao {

	private static final String CONNURL = "jdbc:mysql://localhost:3306/fundtransdb";
	private static final String USERNAME = "root";
	private static final String PASSWORD = "123456";

	@Override
	public PageBean findAll(String page,String num) {
		int totalCount=0;
		List<Fund> fundList=new ArrayList<Fund>();
		Connection conn = DBUtils.getConnection();
		PreparedStatement ps;
		String sql = "select * from fund limit ?,?";
		String sql1="select count(*) as totalCountNum from fund";
		
		try {
			ps = conn.prepareStatement(sql);
			
			ps.setInt(1, (Integer.parseInt(page)-1)*10);
			ps.setInt(2, Integer.parseInt(num));
					
			ResultSet rs=ps.executeQuery();
			System.out.println("ID"+'\t'+"名称"+'\t'+"价格"+'\t'+"简述"
					+'\t'+"状态"+'\t'+"时间");
			while(rs.next()){
				Fund fund=new Fund();
				System.out.println(rs.getString(1)+'\t'+rs.getString(2)
				+'\t'+rs.getString(3)+'\t'+rs.getString(4)+'\t'+rs.getString(5)
				+'\t'+rs.getString(6));
				
				fund.setId(rs.getInt(1));
				fund.setName(rs.getString(2));
				fund.setPrice(rs.getDouble(3));
				fund.setDescription(rs.getString(4));
				fund.setStatus(rs.getString(5));
				fund.setCreateTime(rs.getDate(6));
				fundList.add(fund);
			}
			ps = conn.prepareStatement(sql1);
		    rs=ps.executeQuery();
		    while(rs.next()){
		    	totalCount=rs.getInt(1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PageBean pageBean=new PageBean();
		pageBean.setData(fundList);
		pageBean.setCurPage(Integer.parseInt(page));
		pageBean.setRowsPerPage(Integer.parseInt(num));
		pageBean.setMaxRowCount(totalCount);
		pageBean.countMaxPage();
		return pageBean;
	}

	@Override
	public boolean insert(Fund fund) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getConnection();
		PreparedStatement ps;
		String sql = "insert into fund(name,price,description,status,createTime) values(?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, fund.getName());
			ps.setDouble(2, fund.getPrice());
			ps.setString(3, fund.getDescription());
			ps.setString(4, fund.getStatus());
			ps.setDate(5,DateUtils.GetSqlDate(fund.getCreateTime()));
			return ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(String fundName) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getConnection();
		PreparedStatement ps;
		String sql = "delete from fund where name=?";
		
		try {
			ps = conn.prepareStatement(sql);
			System.out.println(fundName);
			ps.setString(1, fundName);
			return ps.execute();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public boolean update(Fund fund) {
		// TODO Auto-generated method stub
		
		Connection conn = DBUtils.getConnection();
		PreparedStatement ps;
		String sql = "update fund set price=?,description=?,status=? where name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setDouble(1, fund.getPrice());
			ps.setString(2, fund.getDescription());
			ps.setString(3, fund.getStatus());
			ps.setString(4, fund.getName());
			return ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public Fund findById(int fundNo) {
		// TODO Auto-generated method stub
		return null;
	}
}
